package cate.game.play.config;

import easy.java.dev.note.NoteField;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.List;

public class PlayCfgUtil {

	@NoteField("数组分隔符")
	public static final String ARRAY_SEPARATOR = ",";

	@NoteField("字段分割符号")
	public static final String FIELD_SEPARATOR = "&";

	@NoteField("KV分割符号")
	public static final String KV_SEPARATOR = "=";

	@NoteField("list分隔符")
	public static final String LIST_SEPARATOR = "#";

	@NoteField("附加的字段分割符号")
	public static final String EX_FIELD_SEPARATOR = "|";

	@NoteField("附加的kv分割符号")
	public static final String EX_KV_SEPARATOR = "_";


	public static List<PlayCfgArgs> parseList(String code) {
		List<PlayCfgArgs> result = new ArrayList<>();
		if (StringUtils.isNotBlank(code)) {
			for (String s : code.split(ARRAY_SEPARATOR)) {
				result.add(new PlayCfgArgs(s));
			}
		}
		return result;
	}

	/**
	 * 转换附加字段配置
	 *
	 * 开始回合_1|结束回合_2  ----> 开始回合=1&结束回合=2
	 *
	 * @param code
	 * @return
	 */
	public static String replaceExCode(String code){
		if(StringUtils.isBlank(code)){
			return code;
		}
		return code.replace(EX_FIELD_SEPARATOR, FIELD_SEPARATOR).replace(EX_KV_SEPARATOR, KV_SEPARATOR);
	}
}
